package com.gzcstec.web;

import com.gzcstec.domain.Host;
import com.gzcstec.result.Result;
import com.gzcstec.service.HostService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 站点controller类
 */
@RequestMapping("/back/host")
@Controller
public class HostController {

    @Autowired
    private HostService hostService;

    @GetMapping("/getHostById/{hostId}")
    @ResponseBody
    public Result<Host> getById(@PathVariable("hostId")String hostId) {
        return Result.success(hostService.getById(hostId));
    }

    @GetMapping("/getHosts")
    @ResponseBody
    public Result<List<Host>> getList() {
        return Result.success(hostService.getList());
    }

    @PostMapping("/updateHost")
    @ResponseBody
    public Result<Integer> update(@Param("host") Host host) {
        return Result.success(hostService.updateHost(host));
    }

    @PostMapping("/deleteHost/{hostId}")
    @ResponseBody
    public Result<Integer> delete(@PathVariable("hostId")String hostId) {
        return Result.success(hostService.deleteHost(hostId));
    }

    @PostMapping("/addHost")
    @ResponseBody
    public Result<Integer> add(@Param("host") Host host) {
        return Result.success(hostService.addHost(host));
    }

    @RequestMapping("/hosts")
    public String list() {
        return "host/list";
    }

    @RequestMapping("/add")
    public String add() {
        return "host/add";
    }

    @RequestMapping("/edit/{hostId}")
    public String edit(@PathVariable("hostId") String hostId , Model model) {
        Host host = hostService.getById(hostId);
        model.addAttribute("host" , host);
        return "host/edit";
    }

}
